#include "../../common/icsearch.hpp"
#include <gflags/gflags.h>


DEFINE_bool(run_mode,false,"程序的运行模式，false： 调试 true： 发布");
DEFINE_string(log_file,"","发布模式下，向文件中输出，如果是调试模式下，模式是stdout");
DEFINE_int32(log_level,0,"发布模式下，用于指定的日志输出等级");

int main(int argc,char* argv[])
{
    google::ParseCommandLineFlags(&argc,&argv,true);
    init_logger(FLAGS_run_mode,FLAGS_log_file,FLAGS_log_level);

    std::vector<std::string> host_lsit ={"http://127.0.0.1:9200/"};
    auto client = std::make_shared<elasticlient::Client>(host_lsit);

    bool ret = ESIndex(client,"test_user").appendFields("nickname")
    .appendFields("phone","keyword","standard",true)
    .ceeateIndex();

    if(ret == false) {
        LOG_INFO("索引创建失败");
        return -1;
    }else {
        LOG_INFO("索引创建成功");
    }

    //添加数据
    ret = ESInsert(client,"test_user")
    .appendFields("nickname","张三")
    .appendFields("phone","111222333444")
    .insert("0001");

    if(ret == false) {
        LOG_INFO("数据插入失败");
        return -1;
    } else {
        LOG_INFO("数据插入成功");
    }

    ret = ESInsert(client, "test_user")
              .appendFields("nickname", "张三")
              .appendFields("phone", "13344445555")
              .insert("0001");
    if (ret == false)
    {
        LOG_ERROR("数据更新失败!");
        return -1;
    }
    else
    {
        LOG_INFO("数据更新成功!");
    }

    Json::Value user = ESSearch(client, "test_user")
                           .append_should_match("phone.keyword", "13344445555")
                           //.append_must_not_terms("phone.keyword",{"111222333444"})
                           .search();

    if (user.empty() || user.isArray() == false)
    {
        LOG_ERROR("结果为空，或者结果不是数组类型");
        return -1;
    }
    else
    {
        LOG_INFO("数据检索成功!");
    }

    int sz = user.size();
    LOG_DEBUG("检索结果条目数量：{}", sz);
    for (int i = 0; i < sz; i++) {
        LOG_INFO("nickname: {}", user[i]["_source"]["nickname"].asString());
    }

    ret = ESRemove(client, "test_user").remove("0001");
    if (ret == false) {
        LOG_ERROR("删除数据失败");
        return -1;
    }  else {
        LOG_INFO("数据删除成功!");
    }



    return 0;
}